
using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Diagrams;

namespace Dslfactory.wcfLanguage
{
 public partial class wcfLanguageDomainModel
 {
  /// <summary>
  /// Rule changing an Interface into a ServiceContract and vice-versa
  /// for interfaces into the project, and IsInterfaceContact is changed
  /// </summary>
  [RuleOn(typeof(Method))]
  public class Method_OperationContract_Transformations : ChangeRule
  {
   public override void ElementPropertyChanged(ElementPropertyChangedEventArgs e)
   {
    if (e.DomainProperty.Id == Method.IsOperationContractDomainPropertyId)
    {
     Method method = e.ModelElement as Method;
     if (method.IsOperationContract && (method is OperationContract))
      return;
     if ((!method.IsOperationContract) && (!(method is OperationContract)))
      return;


     Method operationContract;
     if (method.IsOperationContract)
      operationContract = new OperationContract(method.Store);
     else
      operationContract = new Method(method.Store);


     Interface iface = method.Interface;
     int index = iface.Methods.IndexOf(method);
     operationContract.Name = method.Name;
     operationContract.Label = method.Label;
     operationContract.IsOperationContract = method.IsOperationContract;
     string signature = method.Signature;
     method.Delete();
     iface.Methods.Insert(index, operationContract);
     // iface.Methods.ReplaceAt(index, operationContract);
     operationContract.Signature = signature;
     //     iface.Methods.Insert(index, operationContract);
    }
   }
  }
 }
}
